﻿using System;
using System.ServiceModel.Channels;
using System.Xml;

namespace NCMIS.ServiceModel.Ra
{
    /// <summary>
    /// Represents a <see cref="System.ServiceModel.Channels.Message"/> that supports emitting XML Declaration
    /// (default WCF behavior of Message class does not support the emitting of XML Declaration).
    /// Source: http://shevaspace.spaces.live.com/blog/cns!FD9A0F1F8DD06954!2012.entry
    /// </summary>
    public class XmlDeclarationMessage : Message
    {
        private Message message;

        /// <summary>
        /// Initializes a new instance of the <see cref="NCMIS.ServiceModel.Ra.XmlDeclarationMessage"/> class.
        /// </summary>
        /// <param name="message">The <see cref="System.ServiceModel.Channels.Message"/> object to base this instance on.</param>
        public XmlDeclarationMessage(Message message)
        {
            this.message = message;
        }

        /// <summary>
        /// Gets the headers of the message.
        /// </summary>
        public override MessageHeaders Headers
        {
            get
            {
                return message.Headers;
            }
        }

        /// <summary>
        /// Called when the message body is written to an XML file.
        /// </summary>
        /// <param name="writer">A <see cref="System.Xml.XmlDictionaryWriter"/> that is used to write this message
        /// body to an XML file.</param>
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            writer.WriteStartDocument();
            message.WriteBodyContents(writer);
        }

        /// <summary>
        /// Gets a set of processing-level annotations to the message.
        /// </summary>
        public override MessageProperties Properties
        {
            get
            {
                return message.Properties;
            }
        }

        /// <summary>
        /// Gets the SOAP version of the message.
        /// </summary>
        public override MessageVersion Version
        {
            get
            {
                return message.Version;
            }
        }
    }
}
